<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{$lang.title}</title>
    <include file="common/tag"/>
</head>
<body>
<div style=" margin: 30px;">

    <div class="layui-form">
        <div style="margin: 30px" id="mform">
            <input type="hidden" id="role_id" value="{$role.role_id}"/>

            <div class="layui-form-item">
                <div class="layui-inline">
                    <div class="layui-inline">
                        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
                            <legend>{$lang_filed['role.name']}:{$role.name}</legend>
                        </fieldset>
                    </div>
                    <div class="layui-inline">
                        <button type="button" class="layui-btn check">{$lang.choose_all}/{$lang.choose_none}</button>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn layui-btn-danger save">{$lang.save}</button>
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <?php foreach ($modules as $moduleName=>$menus) { ?>
                <div class="layui-form-item">
                    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
                        <legend><span class="layui-badge layui-bg-blue"><?=$moduleName?></span></legend>
                    </fieldset>
                    <?php foreach ($menus as $menuName=>$buttons) { ?>
                    <div class="layui-form-item" style="margin-left: 50px">
                        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
                            <legend><?=$menuName?></legend>
                        </fieldset>
                        <div class="layui-input-block">
                            <?php foreach ($buttons as $buttonName) { ?>
                            <input lay-filter="auth" type="checkbox" id="<?=$buttonName?>" value="<?=$buttonName?>" name="auth" title="{$lang[explode('-',$buttonName)[1]]}">
                            <?php } ?>
                        </div>
                    </div>
                    <?php } ?>
                </div>
                <?php } ?>
            </div>
        </div>
    </div>

</div>
</body>
</html>

<script>

    $(function () {
        fii_auth();
        layui.use(['form'], function(){
            let form = layui.form;
            let flag=true;
            $(".check").on('click', function () {
                if (flag){
                    $("input[name='auth']:checkbox").each(function(index, item){
                        item.checked = true;
                    });
                    flag=false;
                }else{
                    $("input[name='auth']:checkbox").each(function(index, item) {
                        item.checked = false;
                    });
                    flag=true;
                }
                form.render('checkbox');
            });
            form.render('checkbox');

        });

        /*
        * 保存权限
        * */
        $(".save").on('click', function () {
            let role_id=$('#role_id').val();
            //获取所有选中的checkbox
            // 获取选中的分类值
            let check_arr = [];
            $('input[name=auth]:checked').each(function(){
                check_arr.push($(this).val());
            });
            if(check_arr.length == 0){
                layer.msg("{$lang.none_resource_choose}");return;
            }

            let loading = layer.load(1, {
                shade: [0.1,'#fff']
            });
            $.post("{$__url}",
                {
                    role_id:role_id,
                    auths:check_arr
                },
                function(data,status){
                    if (data['code']==1){
                        layer.open({
                            title: '{$lang.notice}',
                            content: data['msg']
                            ,yes: function(index, layero){
                                window.parent.location.reload();
                            },cancel: function(){
                                window.parent.location.reload();
                            }
                        });
                    }else{
                        layer.msg(data['msg']);
                    }
                    layer.close(loading);
                }, "json"
            ).error(function(xhr,errorText,errorType){
                let data = eval("("+xhr.responseText+")");
                layer.msg(data['msg']);
                layer.close(loading);
            });
        });
    });

    /*
     * 填充当前角色已有权限列表
     * */
    function fii_auth() {
        let auth=eval('{$auth}')
        for (i=0;i<auth.length;i++){
            $("#"+auth[i]).prop("checked", true);
        }
    }


</script>